package com.yummy.cloud.gateway.config;

import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;

import com.yummy.cloud.gateway.handler.YummyErrorAttributes;

@Configuration
@EnableWebFlux
public class WebMvcConfig implements WebFluxConfigurer {
	
	@Bean
	public YummyErrorAttributes yummyErrorAttributes(ServerProperties serverProperties) {
		YummyErrorAttributes errorAttribures =  new YummyErrorAttributes();
		return errorAttribures;
	}

	@Bean
	public CorsWebFilter corsFilter() {

	    CorsConfiguration config = new CorsConfiguration();
	    config.setAllowCredentials(true);
	    config.addAllowedOrigin("*");
	    config.addAllowedHeader("*");
	    config.addAllowedMethod("*");

	    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	    source.registerCorsConfiguration("/**", config);

	    return new CorsWebFilter(source);
	}
}
